﻿using System;

namespace OneWorks.Web.Common.BaiDu.Place
{
    public class BaiduPlaceEventSearchParams : BaiduPlaceParams
    {
        /// <summary>
        /// 小吃、宾馆、娱乐
        /// </summary>
        public string Query = "";

        /// <summary>
        /// 事件名称，可以是团购、打折或全部，目前只支持团购。 groupon、discount、all
        /// </summary>
        public string Event;

        /// <summary>
        /// 北京、131，注意：和search功能不同，此字段必须填写城市名称或城市代码，不能为全国或1。
        /// </summary>
        public string Region = "北京";

        /// <summary>
        /// 检索中心点，注意顺序：纬度，经度
        /// </summary>
        public string Location = "";

        /// <summary>
        /// 检索半径。默认1000米，最大可设置2000米。
        /// </summary>
        public int Radius = 2000;

        /// <summary>
        ///检索矩形Bound，注意顺序：左下角纬度，左下角经度，右上角纬度，右上角经度。
        /// </summary>
        public string Bounds = "";

        /// <summary>
        /// 检索过滤条件。
        /// </summary>
        public BaiduPlaceEventSearchParamFilter EventSearchParamFilter;

        /// <summary>
        /// 返回记录数量，默认为10条记录，最大返回结果为20条。
        /// </summary>
        public int PageSize = 10;
        /// <summary>
        /// 分页页码，默认为0,0代表第一页，1代表第二页，以此类推。
        /// </summary>
        public int PageNum = 1;

        public override string ToString()
        {
            if (string.IsNullOrWhiteSpace(Query) || string.IsNullOrWhiteSpace(Event) || string.IsNullOrWhiteSpace(Region) &&
                (string.IsNullOrWhiteSpace(Location) || string.IsNullOrWhiteSpace(Bounds)))
                throw new ArgumentException(BaiduErrorMessages.LackParam);

            string s = "";

            s += "&query=" + System.Web.HttpUtility.UrlEncode(Query);

            s += "&event=" + Event;

            s += "&region=" + System.Web.HttpUtility.UrlEncode(Region);

            if (!string.IsNullOrWhiteSpace(Location))
                s += "&location=" + Location;

            s += "&radius=" + Radius;

            if (!string.IsNullOrWhiteSpace(Bounds))
                s += "&bounds=" + Bounds;

            s += "&page_size=" + PageSize;

            s += "&page_num=" + PageNum;

            if (EventSearchParamFilter != null)
            {
                s += "&filter=" + EventSearchParamFilter;
            }

            return base.ToString() + s;
        }
    }

    public class BaiduPlaceEventSearchParamFilter
    {

        public BaiduPlaceEventSearchGrouponType GrouponType;

        /// <summary>
        /// 
        /// </summary>
        public int SortRule;

        public BaiduPlaceEventSearchDataType SortName;

        public override string ToString()
        {
            switch (SortName)
            {
                case BaiduPlaceEventSearchDataType.DataType:
                    return string.Format("groupon_type:{0}|sort_name:{1}|sort_rule:{2}", GrouponType, "data_type",
                                         SortRule);
                case BaiduPlaceEventSearchDataType.Distance:
                    return string.Format("groupon_type:{0}|sort_name:{1}|sort_rule:{2}", GrouponType, "distance",
                                         SortRule);
                case BaiduPlaceEventSearchDataType.GrouponNum:
                    return string.Format("groupon_type:{0}|sort_name:{1}|sort_rule:{2}", GrouponType, "groupon_num",
                                         SortRule);
                case BaiduPlaceEventSearchDataType.GrouponPrice:
                    return string.Format("groupon_type:{0}|sort_name:{1}|sort_rule:{2}", GrouponType, "groupon_price",
                                         SortRule);
                case BaiduPlaceEventSearchDataType.GrouponStart:
                    return string.Format("groupon_type:{0}|sort_name:{1}|sort_rule:{2}", GrouponType, "groupon_start",
                                         SortRule);
                default:
                    goto case BaiduPlaceEventSearchDataType.DataType;
            }
        }
    }

    public enum BaiduPlaceEventSearchDataType
    {
        /// <summary>
        /// 数据类型
        /// </summary>
        DataType,
        /// <summary>
        /// 团购销量
        /// </summary>
        GrouponNum,
        /// <summary>
        /// 团购价格
        /// </summary>
        GrouponPrice,
        /// <summary>
        /// 团购开始时间
        /// </summary>
        GrouponStart,
        /// <summary>
        /// 按距离排序，只对中心点检索有效
        /// </summary>
        Distance
    }

    public enum BaiduPlaceEventSearchGrouponType
    {
        餐饮 = 1,
        生活 = 2,
        娱乐 = 3,
        旅游住宿 = 4
    }
}
